package com.WFinstitute.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.WFinstitute.domain.Scholar;
import com.WFinstitute.domain.ScholarArticle;
import com.WFinstitute.service.ScholarArticleService;
import com.WFinstitute.service.ScholarService;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.TimeUtil;
import com.WFinstitute.util.TrimHtmlUtil;


/**
 * 
 * @author 匹夫
 * @date 2016-7-31 上午10:14:43
 * @function
 */

@Controller
@RequestMapping("/scholararticle")
public class ScholarArticleBackController {
	@Resource
	private ScholarArticleService  scholarArticleService;
	@Resource
	private ScholarService scholarService;
	
	/**
	 * 【后台页面】学者文章分页列表界面
	 * @param response
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/article_list")
	public Map<String, Object> article_list(HttpServletResponse response,HttpServletRequest request){
		//获取页面传来每页显示条数
        int row=request.getParameter("rows")!=null?Integer.valueOf(request.getParameter("rows")):10;
        //获取页面传来当前页码
        int page=request.getParameter("page")!=null?Integer.valueOf(request.getParameter("page")):1;
        //计算一共有多少条
        int count = (int)scholarArticleService.count();
		Page<ScholarArticle> pageList = scholarArticleService.findScholarArticleList(row, page, "");
		List<ScholarArticle> scholarArticleList = pageList.getList();
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("rows", scholarArticleList);
		result.put("total", count);
		return result;
	}
	
	
	//为后台select标签服务，传递学者名字
	@RequestMapping("/scholar_list")
	public String scholarlist(HttpServletResponse response,HttpServletRequest request){
		//得到学者的信息
		List<Scholar> scholarList = scholarService.findScholarList();
		request.setAttribute("scholarList", scholarList);
		request.setAttribute("statelist", 1);
		return "/WEB-INF/backstage/scholararticle/list.jsp";
	}
	
	

	@RequestMapping("/addscholararticle")
	public void addscholararticle(HttpServletResponse response,HttpServletRequest request){
		 response.setCharacterEncoding("UTF-8");
			JSONObject jo=new JSONObject();
		try {
		//图片上传路径
		String articletitle = request.getParameter("articletitle");
		String scholarname = request.getParameter("sholar");
		String content = request.getParameter("content");
		if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")){
			int a = Integer.parseInt(request.getParameter("id"));
			ScholarArticle scholarArticle = scholarArticleService.findScholarArticleById(a);
			scholarArticle.setArticlecontent(content);
			scholarArticle.setArticletitle(articletitle);
			scholarArticle.setCreatetime(TimeUtil.getCurrentTime());
			scholarArticle.setReadingcount(0);
			scholarArticleService.updateScholarArticle(scholarArticle);
		}else{
			ScholarArticle scholarArticle=new ScholarArticle();
			scholarArticle.setArticlecontent(content);
			scholarArticle.setArticletitle(articletitle);
			scholarArticle.setCreatetime(TimeUtil.getCurrentTime());
			scholarArticle.setScholarname(scholarname);
			scholarArticleService.addScholarArticle(scholarArticle);
		}
		jo.put("state", "0");
		response.getWriter().write(jo.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 【后台页面】删除文章
	 * @param response
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/deletescholararticle")
	public void deletescholar(HttpServletResponse response,HttpServletRequest request,ScholarBackController scholarBackController) throws IOException{
            if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")){
            	scholarArticleService.deleteScholarArticleById(Integer.parseInt(request.getParameter("id")));
            	JSONObject jo=new JSONObject();
            	jo.put("state", "0");
            	response.getWriter().print(jo);
            }
	}	
	
	
}
